﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class UserRoles
    {
        DbHelperSQL dbhelper = new DbHelperSQL();

        #region 获取全部数据，返回实体对象集合
        /// <summary>
        /// 获取全部数据，返回实体对象集合
        /// </summary>
        /// <returns></returns>
        public List<MODEL.UserRoles> getModel()
        {
            string sql = "select * from UserRoles;";
            DataTable dt = dbhelper.GetDataTable(sql);
            if (dt.Rows.Count <= 0)
            {
                return null;
            }
            else
            {
                List<MODEL.UserRoles> list = new List<MODEL.UserRoles>();
                foreach (DataRow dr in dt.Rows)
                {
                    MODEL.UserRoles model = new MODEL.UserRoles();
                    setDr2Model(dr, model);
                    list.Add(model);
                }
                return list;
            }
        } 
        #endregion

        #region 将数据行转换成实体对象
        /// <summary>
        /// 将数据行转换成实体对象
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="model"></param>
        public void setDr2Model(DataRow dr, MODEL.UserRoles model)
        {
            if (dr.Table.Columns.Contains("UrId") && !string.IsNullOrEmpty(dr["UrId"].ToString()))
            {
                model.UrId = Convert.ToInt32(dr["UrId"]);
            }
            if (dr.Table.Columns.Contains("UrName") && !string.IsNullOrEmpty(dr["UrName"].ToString()))
            {
                model.UrName = dr["UrName"].ToString();
            }
        } 
        #endregion
    }
}
